import React, { Component } from 'react';
import CallSearch from './CallSearch';
import CallTable from './CallTable';
import api from '../../../api/api';
import './index.scss';

class CallList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: [],
      searchData: {},
      pageSize:10,
      pageIndex:1,
      taskTypeList:[]
    };
    this.taskType = undefined
    this.patientName = undefined
    this.startDate = undefined
    this.endDate = undefined

  }
  componentWillMount() {
    this.searchData({patientName:this.props.customerName},true);
    this.getTaskTypeListFun()
  }
  async getTaskTypeListFun(){
    const getTaskTypeList = await api.getTaskType()
    this.setState({
      taskTypeList : getTaskTypeList
    })
  }
  searchData = async (data,submitClick) => {
    if(data){
      for(let i in data){
        if(data[i] == ""){
          data[i] = undefined
        }
      }
    }
    if(submitClick){
      this.setState({
        searchData:data
      })
    }else{
      data = this.state.searchData
    }
    // this.props.history.push(`/callList?taskType=${data.taskType}&patientName=${data.patientName}&startDate=${data.startDate}&endDate=${data.endDate}`)
    const pageIndex = submitClick ? 1:this.state.pageIndex
    const pageSize = this.state.pageSize
    const skip = pageSize * (pageIndex - 1);
    const limit = pageSize;
    let tableData  = {}
    tableData.list = await api.getCallList(data, skip, limit + 1);
    const getTaskTypeList = await api.getTaskType()
    if (tableData.list) {
        if (tableData.list.length > limit) {
            tableData.count = skip + limit + 1;
            tableData.list = tableData.list.slice(0, limit);
        } else {
            tableData.count = skip + (tableData.list ? tableData.list.length : 0);
        }
    } else {//兼容不存在list属性的数据结构
        tableData.count = skip + (tableData.length ? tableData.length : 0);
        tableData.list = Array.isArray(tableData) && tableData.slice(0, limit);
    }
    tableData.pageIndex = Number(submitClick ? 1:this.state.pageIndex)
    tableData.pageSize = Number(this.state.pageSize)
    if(tableData.list && tableData.list.length>0){
      // 匹配一级分类
      for(let i in tableData.list){
        if(tableData.list[i].taskType || tableData.list[i].taskType == "0"){
          for(let y in getTaskTypeList){
            if(getTaskTypeList[y].id == tableData.list[i].taskType){
              tableData.list[i].taskType = getTaskTypeList[y].name
              break
            }
          }
          // 匹配二级分类
          if(tableData.list[i].taskReclassify){
            for(let y in getTaskTypeList){
              if(getTaskTypeList[y].secondaryType && getTaskTypeList[y].secondaryType.length>0){
                for(let z in getTaskTypeList[y].secondaryType){
                  if(tableData.list[i].taskReclassify == getTaskTypeList[y].secondaryType[z].id){
                    tableData.list[i].taskReclassify = getTaskTypeList[y].secondaryType[z].name
                    break
                  }
                }
              }
            }
          }
        }
      }
    }
    this.setState({
      tableData: tableData,
    });
  };
  pageChange=(pagination)=>{
    // console.log('pagination:',pagination)
    const pageSize = pagination.pageSize;
    const pageIndex = pagination.current;
    this.setState({
      pageSize:pageSize,
      pageIndex:pageIndex
    })
    setTimeout(()=>{
      this.searchData({})
    },0)
  }
  getTableChildMsg = () => {
    this.searchData()
  }


  render() {
    return (
      <div>
        {this.state.taskTypeList.length>0?<div><CallSearch searchData={this.searchData} taskTypeList={this.state.taskTypeList} ref="childrenSearch"></CallSearch>
        <CallTable
          tableData={this.state.tableData}
          taskTypeList={this.state.taskTypeList}
          pageChange={this.pageChange}
          ref="childrenTable"
          parent={ this }
        ></CallTable></div>:null}
      </div>
    );
  }
}

export default CallList;
